﻿#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;

// 用户类
class User {
protected:
    string username;
    string password;
    bool isAdmin;
public:
    User(string name="", string pwd="", bool admin=false) : 
        username(name), password(pwd), isAdmin(admin) {}
    
    string getUsername() const { return username; }
    string getPassword() const { return password; } // 添加获取密码的方法
    bool getIsAdmin() const { return isAdmin; }
    bool checkPassword(string pwd) const { return password == pwd; }
};

// 活动类
class Activity {
private:
    string title;
    string time;
    string location;
    int maxParticipants;
    vector<string> participants;
public:
    Activity(string t="", string tm="", string loc="", int max=0) :
        title(t), time(tm), location(loc), maxParticipants(max) {}
    
    bool addParticipant(string username) {
        if(participants.size() >= static_cast<size_t>(maxParticipants)) return false; // 修复size_t到int的转换
        participants.push_back(username);
        return true;
    }
    
    bool removeParticipant(string username) {
        for(auto it = participants.begin(); it != participants.end(); ++it) {
            if(*it == username) {
                participants.erase(it);
                return true;
            }
        }
        return false;
    }
    
    // Getters
    string getTitle() const { return title; }
    string getTime() const { return time; }
    string getLocation() const { return location; }
    int getMaxParticipants() const { return maxParticipants; }
    int getCurrentParticipants() const { return static_cast<int>(participants.size()); } // 修复size_t到int的转换
    vector<string> getParticipants() const { return participants; }
};

// 系统类
class System {
private:
    vector<User> users;
    vector<Activity> activities;
    User* currentUser;
    
    void loadUsers() {
        ifstream file("users.txt");
        string username, password;
        bool isAdmin;
        while(file >> username >> password >> isAdmin) {
            users.push_back(User(username, password, isAdmin));
        }
        file.close();
    }
    
    void saveUsers() {
        ofstream file("users.txt");
        for(const User& user : users) {
            file << user.getUsername() << " " << user.getPassword() 
                 << " " << user.getIsAdmin() << endl;
        }
        file.close();
    }

public:
    System() : currentUser(nullptr) {
        loadUsers();
    }
    
    bool registerUser(string username, string password) {
        // 检查用户名是否已存在
        for(const User& user : users) {
            if(user.getUsername() == username) return false;
        }
        users.push_back(User(username, password, false));
        saveUsers();
        return true;
    }
    
    bool login(string username, string password) {
        for(User& user : users) {
            if(user.getUsername() == username && user.checkPassword(password)) {
                currentUser = &user;
                return true;
            }
        }
        return false;
    }
    
    void addActivity(string title, string time, string location, int maxParticipants) {
        if(currentUser && currentUser->getIsAdmin()) {
            activities.push_back(Activity(title, time, location, maxParticipants));
        }
    }
    
    bool signUpActivity(string title) {
        if(!currentUser || currentUser->getIsAdmin()) return false;
        
        for(Activity& activity : activities) {
            if(activity.getTitle() == title) {
                return activity.addParticipant(currentUser->getUsername());
            }
        }
        return false;
    }
    
    void listActivities() {
        for(const Activity& activity : activities) {
            cout << "活动: " << activity.getTitle() << endl
                 << "时间: " << activity.getTime() << endl
                 << "地点: " << activity.getLocation() << endl
                 << "已报名人数: " << activity.getCurrentParticipants() 
                 << "/" << activity.getMaxParticipants() << endl << endl;
        }
    }
    
    void exportActivities() {
        if(!currentUser || !currentUser->getIsAdmin()) return;
        
        ofstream file("activity.txt");
        for(const Activity& activity : activities) {
            file << "活动: " << activity.getTitle() << endl
                 << "时间: " << activity.getTime() << endl
                 << "地点: " << activity.getLocation() << endl
                 << "报名人数: " << activity.getCurrentParticipants() 
                 << "/" << activity.getMaxParticipants() << endl;
            
            file << "报名名单: " << endl;
            for(const string& participant : activity.getParticipants()) {
                file << participant << endl;
            }
            file << "----------------------" << endl;
        }
        file.close();
    }
};

int main() {
    System sys;
    string username, password;
    int choice;
    
    while(true) {
        cout << "欢迎使用活动报名管理系统" << endl;
        cout << "1. 用户注册" << endl;
        cout << "2. 用户登录" << endl;
        cout << "0. 退出系统" << endl;
        cout << "请选择: ";
        cin >> choice;
        
        switch(choice) {
            case 1: {
                cout << "请输入用户名: ";
                cin >> username;
                cout << "请输入密码: ";
                cin >> password;
                if(sys.registerUser(username, password)) {
                    cout << "注册成功！" << endl;
                } else {
                    cout << "用户名已存在！" << endl;
                }
                break;
            }
            case 2: {
                cout << "请输入用户名: ";
                cin >> username;
                cout << "请输入密码: ";
                cin >> password;
                if(sys.login(username, password)) {
                    cout << "登录成功！" << endl;
                    // 这里可以添加登录后的功能菜单
                    while(true) {
                        cout << "请选择操作: " << endl;
                        cout << "1. 查看活动" << endl;
                        cout << "2. 报名活动" << endl;
                        cout << "0. 退出系统" << endl;
                    }
                } else {
                    cout << "用户名或密码错误！" << endl;
                }
                break;
            }
            case 0:
                return 0;
            default:
                cout << "无效选择！" << endl;
        }
    }
    
    return 0;
}
